package com.bytedance.social.service.impl;

import com.bytedance.social.common.JwtUtil;
import com.bytedance.social.common.StatusCode;
import com.bytedance.social.http.Result;
import com.bytedance.social.http.Root;
import com.bytedance.social.mapper.CommentMapper;
import com.bytedance.social.mapper.SocailMapper;
import com.bytedance.social.pojo.Comment;
import com.bytedance.social.pojo.User;
import com.bytedance.social.request.CommentActionRequest;
import com.bytedance.social.request.CommentListRequest;
import com.bytedance.social.respones.CommentActionResponse;
import com.bytedance.social.respones.CommentListResponse;
import com.bytedance.social.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SocailMapper socailMapper;

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 登录用户对视频进行评论
     *
     * @param commentActionRequest
     * @return
     */
    @Override
    @Transactional
    public CommentActionResponse userCommentAction(CommentActionRequest commentActionRequest) {
        String token = commentActionRequest.getToken();
        Long userId = JwtUtil.parseToken(token);
        Long videoId = commentActionRequest.getVideo_id();
        CommentActionResponse commentActionResponse = new CommentActionResponse();
        // 1-发布评论，2-删除评论
        Integer actionType = commentActionRequest.getAction_type();
        //查询video的评论总数把评论总数查询出来
        Long videoCommentCount = commentMapper.selectVideoCommentCount(videoId);
        if (actionType == 1) {
            Comment comment = new Comment();
            User user = socailMapper.selectUserById(userId);
            comment.setUser(user);
            comment.setUser_id(userId);
            comment.setContent(commentActionRequest.getComment_text());
            String systemTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
            String ip = httpServletRequest.getHeader("userfrom");
            //调用接口转换ip
            Root forObject = restTemplate.getForObject("https://apis.map.qq.com/ws/location/v1/ip?ip=" + ip + "&key=LLPBZ-GTKRQ-HGH5M-BRHYO-IC7LQ-FEBZP", Root.class);
            System.out.println(forObject);
            Result result = forObject.getResult();
            if (result != null) {
                comment.setCreate_date(systemTime + "-from:" + result.getAd_info().getProvince() + result.getAd_info().getCity() + result.getAd_info().getDistrict());
            } else {
                comment.setCreate_date(systemTime + "-from:" + ip);
            }
            comment.setVideo_id(videoId);
            commentMapper.userPulishComment(comment);
            //发布评论成功后去修改，video表的评论总数
            commentMapper.updateComments(videoId, videoCommentCount + 1);
            commentActionResponse.setComment(comment);
            commentActionResponse.setStatus_code(StatusCode.STATUS_OK);
            commentActionResponse.setStatus_msg("评论成功！");
            return commentActionResponse;
        } else if (actionType == 2) {
            //修改video表的评论总数
            commentMapper.updateComments(videoId, videoCommentCount - 1);
            //删除评论表
            Long commentId = commentActionRequest.getComment_id();
            commentMapper.deleteUserComment(commentId);
            commentActionResponse.setStatus_code(StatusCode.STATUS_OK);
            commentActionResponse.setStatus_msg("删除成功！");
        }
        return commentActionResponse;
    }

    /**
     * 查看视频的所有评论，按发布时间倒序。
     *
     * @param commentListRequest
     * @return
     */
    @Override
    public CommentListResponse getVideoComments(CommentListRequest commentListRequest) {
        Long videoId = commentListRequest.getVideo_id();
        CommentListResponse commentListResponse = new CommentListResponse();
        List<Comment> commentList = commentMapper.selectVideoComments(videoId);
        for (Comment comment : commentList) {
            Long user_id = comment.getUser_id();
            User user = socailMapper.selectUserById(user_id);
            comment.setUser(user);
        }
        commentListResponse.setComment_list(commentList);
        commentListResponse.setStatus_code(StatusCode.STATUS_OK);
        commentListResponse.setStatus_msg("查看视频的评论成功！");
        return commentListResponse;
    }
}
